package com.hy.mvc.biz.excel.enums;

import com.hy.mvc.biz.utils.DateUtils;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Booker on 2017/7/3.
 */
public enum DataType {
    BIGDECIMAL {
        @Override
        public Object convertDataType(Object value) {
            if (value instanceof Double) {
                return new BigDecimal(((Double) value).longValue());
            }
            return new BigDecimal(value.toString());
        }
    },
    BIGINTEGER {
        @Override
        public Object convertDataType(Object value) {
            if (value instanceof Double) {
                return BigInteger.valueOf(((Double) value).longValue());
            }
            return BigInteger.valueOf(Long.parseLong(value.toString()));
        }
    },
    BOOLEAN {
        @Override
        public Object convertDataType(Object value) {
            if (value instanceof Boolean) {
                return value;
            }
            return new Boolean(value.toString());
        }
    },
    BYTE {
        @Override
        public Object convertDataType(Object value) {
            if (value instanceof Double) {
                return ((Double) value).byteValue();
            }
            return new Byte(value.toString());
        }
    },
    CALENDAR {
        @Override
        public Object convertDataType(Object value) {
            Calendar calendar = Calendar.getInstance();
            if (value.getClass().isAssignableFrom(Date.class)) {
                calendar.setTime((Date) value);
            } else {
                calendar.setTime(DateUtils.parse4y2M2d(value.toString()));
            }
            return calendar;
        }
    },
    CHARACTER {
        @Override
        public Object convertDataType(Object value) {
            if (StringUtils.isNotEmpty(value.toString())) {
                return new Character(value.toString().charAt(0));
            }
            return null;
        }
    },
    DATE {
        @Override
        public Object convertDataType(Object value) {
            if (value.getClass().isAssignableFrom(Date.class)) {
                return value;
            }
            return DateUtils.parse4y2M2d((value.toString()));
        }
    },
    DOUBLE {
        @Override
        public Object convertDataType(Object value) {
            if (value instanceof Double) {
                return ((Double) value).doubleValue();
            }
            return Double.parseDouble(value.toString());
        }
    },
    FLOAT {
        @Override
        public Object convertDataType(Object value) {
            if (value instanceof Double) {
                return ((Double) value).floatValue();
            }
            return Float.parseFloat(value.toString());
        }
    },
    INTEGER {
        @Override
        public Object convertDataType(Object value) {
            if (value instanceof Double) {
                return ((Double) value).intValue();
            }
            return new Integer(value.toString());
        }
    },
    LONG {
        @Override
        public Object convertDataType(Object value) {
            if (value instanceof Double) {
                return ((Double) value).longValue();
            }
            return Long.parseLong(value.toString());
        }
    },
    SHORT {
        @Override
        public Object convertDataType(Object value) {
            if (value instanceof Double) {
                return ((Double) value).shortValue();
            }
            return Short.parseShort(value.toString());
        }
    },
    STRING {
        @Override
        public Object convertDataType(Object value) {
            return value.toString();
        }
    },
    TIME {
        @Override
        public Object convertDataType(Object value) {
            if (value instanceof Double) {
                return new Time(((Double) value).longValue());
            }
            return new Time(Long.parseLong(value.toString()));
        }
    },
    TIMESTAMP {
        @Override
        public Object convertDataType(Object value) {
            if (value instanceof Double) {
                return new Timestamp(((Double) value).longValue());
            }
            return new Timestamp(Long.parseLong(value.toString()));
        }
    };

    public abstract Object convertDataType(Object value);
}
